/*
 * FQL Builder
 *
 * http://fql-builder.mariogarcia.biz
 * http://www.facebook.com/mariogarcia.ar 
 *
 * Copyright (c) 2011 - 2012 Mario Garcia
 *
 * 
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */

// Global Variables
var _wrapper;
var _sidebar;
var _fb_tables;
var _panel;
var _console;
var _console_query;
var _fb_tables_desktop = {};
var _z_index = 100;
var _console_run;

// DOM is ready
jQuery(function(){

    //Init var
    _wrapper = jQuery('#wrapper');
    _sidebar = jQuery('#sidebar');
    _fb_tables = jQuery('#fb-tables');
    _panel = jQuery('#panel');
    _console = jQuery('#console');
    _console_query = jQuery('#console_query');
    _console_run = jQuery('#console_run');
    initTree();
    initRun();
});


/**
 *  Init TreeView 
 *  
 *  Tables initialization   
 *
 */ 
function initTree(){
    
    function _onActivate(node){
        createFBTable(node.data.title)
    }
    
    var _config = {
        onActivate: _onActivate,
        initAjax: {
            url: "data/fb-tables.php"
        }
    }
    _fb_tables.dynatree(_config);
} 


/**
 *  Create FB Table
 *
 */  
function createFBTable(pName){
    if(_fb_tables_desktop[pName]){
        var _ele = _fb_tables_desktop[pName];
        _ele.css('top',0).css('left',0);
        return false;
    }

    // url 
    var _url = 'data/fb-create-table.php';
    
    // params to ajax call
    var _data = {
        'table_name':pName
    };
    
    
    _panel.block();
    $.post(_url,_data,_onSuccess);
    
    function _onSuccess(pData){
        _panel.unblock();
        
        // init var
        var _ele = jQuery(pData);
        _fb_tables_desktop[pName] = _ele;
        
        // add to panel
        _panel.prepend(_ele); 
                
        var _ele_close = jQuery('.wd-fb-table-min',_ele);
        var _ele_build = jQuery('.wd-fb-table-build',_ele);
        var _ele_help = jQuery('.wd-fb-table-help',_ele);
        
        var _ele_table = jQuery('.wd-fb-table-table',_ele);
        
        var _ele_panel = jQuery('.wd-fb-table-panel',_ele);
        var _ele_all = jQuery('.wd-fb-table-all',_ele);
        var _ele_check = jQuery('.fb-checkbox',_ele);
                         
        
        // add drag support
        var _opt = {
            stack:'.wd-fb-table',
            cursor: 'move'
        };
        _ele.draggable(_opt);
        
        // close fields        
        _ele_close.click(function(e){
            _ele_panel.toggle();  
        })
        
        
        // tooltip help
        jQuery(".wd-fb-table-header a",_ele).tooltip({
            bodyHandler: function() {
                return jQuery(jQuery(this).attr("href")).html();
            },
            showURL: false
        });
        
        jQuery("a",_ele_table).tooltip({
            bodyHandler: function() {
                return jQuery(jQuery(this).attr("href")).html();
            },
            showURL: false
        });

        // Build the query
        _ele_build.click(function(e){
            var _checked = jQuery('input.fb-checkbox:checked',_ele);
            var _checked_indexable = jQuery('input.fb-checkbox-indexable',_ele);
            var _fields = '';
            var _fields_indexable = '';
            var _out = '';
            
            var _first = true;
            
            _checked.each(function(i){
                var _this = jQuery(this);
                _fields += (_first?'':', ') + _this.val() ;
                _first = false;
            })

            var _first = true;
            
            _checked_indexable.each(function(i){
                var _this = jQuery(this);
                _fields_indexable += (_first?'':' and ') + _this.val() + ' = ? ' ;
                _first = false;
            })
                        
            _out += 'select '+ _fields +' from '+ _ele.attr('customtitle') +' where '+ _fields_indexable +' \n';	
            _console_query.val(_out);
        })

        // select all fields
        _ele_all.change(function(e){
            var _checkboxs = jQuery('input:checkbox',_ele);
            _checkboxs.attr('checked', _ele_all.attr('checked')=='checked' );
        }); 
        _ele_check.click(function(e){
            _ele_all.attr('checked', false );
        });  
        
        // z-index    
   
        _ele.click(function(e){
            _ele.css('z-index',_z_index++);
        });
    
    }        
        
}


function initRun(){
    _console_run.click(function(){
        var _data = {
            'query':_console_query.val()
        }
        _wrapper.block();
        $.post("data/fb-run.php", _data, _onLoad);
        
        function _onLoad(data){
            _wrapper.unblock();
            var _data = jQuery(data);
            var _config = {
                message: _data
                , css: {
                    width: '90%',
                    height: '400px'
                }
            }
            _wrapper.block(_config);         
        }
      
    });
}